home *** CD-ROM | disk | FTP | other *** search
-
- { This program will draw a set of squares nested one inside the
- next. The user specifies how many squares are to be drawn.
- A discussion of the design of the program is given in
- Subsection 6.3.2 of "The Most Complex Machine." }
-
- DECLARE HowMany { the number of squares to be drawn,
- as specified by the user }
-
- DECLARE count { counter, used to keep track of the number of
- squares drawn so far }
-
- DECLARE length { the length of the side of one of the squares;
- this is increased after each square is drawn }
-
-
- AskUser("How many squares do you want to draw (1 to 10)?", HowMany)
-
- { The next two statements set things up for the FIRST square to
- be drawn; this square will have its lower-left corner at the
- point (0,0), and the length of its side will be 1 }
-
- count := 0 { no squares drawn yet }
-
- length := 1 { the length of the side of the first square to be drawn }
-
- LOOP
-
- forward(length) { draw a square }
- turn(90)
- forward(length)
- turn(90)
- forward(length)
- turn(90)
- forward(length)
-
- count := count + 1 { update counter and check if done }
- EXIT IF count = HowMany
-
- { The rest of the loop sets things up for the NEXT square,
- which will be drawn when the computer returns to the
- beginning of the loop. The preconditions that are being set
- up here are:
- (1) The value of the variable, length, is equal to the
- side of the square that is to be drawn next;
- (2) The turtle is at the lower left corner of that square;
- (3) The turtle is facing to the right.
- (4) The pen must be Down.
- }
-
- length := length + 2 { each square is 2 units bigger than the last }
-
- PenUp { to move turtle without drawing a line, pen must be Up }
- Move(-1,-1) { move to position of lower left corner of next square }
- PenDown
-
- face(0) { face right }
-
- END LOOP